package com.grade.web.controller.system;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.grade.common.core.domain.Ztree;
import com.grade.common.core.domain.entity.SysRole;
import com.grade.common.utils.ShiroUtils;
import com.grade.system.domain.GradeTemplate;
import com.grade.system.domain.GradeType;
import com.grade.system.service.IGradeTemplateService;
import com.grade.system.service.IGradeTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.grade.common.annotation.Log;
import com.grade.common.enums.BusinessType;
import com.grade.system.domain.GradeItem;
import com.grade.system.service.IGradeItemService;
import com.grade.common.core.controller.BaseController;
import com.grade.common.core.domain.AjaxResult;
import com.grade.common.utils.poi.ExcelUtil;
import com.grade.common.core.page.TableDataInfo;

/**
 * 评分指标项Controller
 * 
 * @author grade
 * @date 2021-10-17
 */
@Controller
@RequestMapping("/system/item")
public class GradeItemController extends BaseController
{
    private String prefix = "system/item";

    @Autowired
    private IGradeItemService gradeItemService;

    @Autowired
    private IGradeTypeService gradeTypeService;

    @Autowired
    private IGradeTemplateService gradeTemplateService;

    @RequiresPermissions("system:item:view")
    @GetMapping()
    public String item(Long templateId,Long typeId,ModelMap mmap)
    {
        GradeItem gradeItem = new GradeItem();
        if(templateId!=null&& templateId>0){
            gradeItem.setTemplateId(templateId);
            mmap.put("selectedTemplateId", templateId);
        }
        if(typeId!=null&& typeId>0){
            gradeItem.setTypeId(typeId);
            mmap.put("selectedTypeId", typeId);
        }
        mmap.put("gradeItem", gradeItem);
        List<GradeTemplate> templateList = gradeTemplateService.selectGradeTemplateList(null);
        mmap.put("templateList", JSONObject.toJSON(templateList));

        GradeType gradeTypeCondition = new GradeType();
        gradeTypeCondition.setTemplateId(templateId);
        List<GradeType> typeList = gradeTypeService.getGradeTypeOptionList(gradeTypeCondition);
        mmap.put("list", JSONObject.toJSON(typeList));
        return prefix + "/item";
    }

    /**
     * 查询评分指标项列表
     */
    @RequiresPermissions("system:item:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GradeItem gradeItem)
    {
        startPage();
        List<GradeItem> list = gradeItemService.selectGradeItemList(gradeItem);
        return getDataTable(list);
    }

    /**
     * 导出评分指标项列表
     */
    @RequiresPermissions("system:item:export")
    @Log(title = "评分指标项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GradeItem gradeItem)
    {
        List<GradeItem> list = gradeItemService.selectGradeItemList(gradeItem);
        ExcelUtil<GradeItem> util = new ExcelUtil<GradeItem>(GradeItem.class);
        return util.exportExcel(list, "评分指标项数据");
    }

    /**
     * 新增评分指标项
     */
    @GetMapping("/add")
    public String add(Long templateId,Long typeId,ModelMap mmap)
    {
        mmap.put("selectedTemplateId", templateId);
        List<GradeTemplate> templateList = gradeTemplateService.selectGradeTemplateList(null);
        mmap.put("templateList", JSONObject.toJSON(templateList));
        mmap.put("selectedTypeId", typeId);
        GradeType gradeTypeCondition = new GradeType();
        gradeTypeCondition.setTemplateId(templateId);
        List<GradeType> typeList = gradeTypeService.getGradeTypeOptionList(gradeTypeCondition);
        mmap.put("list", JSONObject.toJSON(typeList));
        return prefix + "/add";
    }

    /**
     * 新增保存评分指标项
     */
    @RequiresPermissions("system:item:add")
    @Log(title = "评分指标项", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GradeItem gradeItem)
    {
        String deductFlag = gradeItem.getDeductFlag();
        if("on".equals(deductFlag)){
            gradeItem.setDeductFlag("0");
        }else{
            gradeItem.setDeductFlag("1");
        }

        return toAjax(gradeItemService.insertGradeItem(gradeItem));
    }

    /**
     * 修改评分指标项
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        List<GradeTemplate> templateList = gradeTemplateService.selectGradeTemplateList(null);
        mmap.put("templateList", JSONObject.toJSON(templateList));

        GradeItem gradeItem = gradeItemService.selectGradeItemById(id);
        mmap.put("gradeItem", gradeItem);

        GradeType gradeTypeCondition = new GradeType();
        gradeTypeCondition.setTemplateId(gradeItem.getTemplateId());
        //mmap.put("selectedTypeId", gradeItem.getTypeId());
        List<GradeType> typeList = gradeTypeService.getGradeTypeOptionList(gradeTypeCondition);
        mmap.put("list", JSONObject.toJSON(typeList));

        //mmap.put("selectedTemplateId", gradeItem.getTemplateId());

        return prefix + "/edit";
    }

    /**
     * 修改保存评分指标项
     */
    @RequiresPermissions("system:item:edit")
    @Log(title = "评分指标项", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GradeItem gradeItem)
    {
        String deductFlag = gradeItem.getDeductFlag();
        if("on".equals(deductFlag)){
            gradeItem.setDeductFlag("0");
        }else{
            gradeItem.setDeductFlag("1");
        }
        return toAjax(gradeItemService.updateGradeItem(gradeItem));
    }

    /**
     * 删除评分指标项
     */
    @RequiresPermissions("system:item:remove")
    @Log(title = "评分指标项", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gradeItemService.deleteGradeItemByIds(ids));
    }

    /**
     * 加载角色菜单列表树
     */
    @GetMapping("/itemTreeData")
    @ResponseBody
    public List<Ztree> itemTreeData(SysRole role)
    {
        Long userId = ShiroUtils.getUserId();
        List<Ztree> ztrees = gradeItemService.itemTreeData(userId);
        return ztrees;
    }
    /**
     * 状态修改
     */
    @Log(title = "状态管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:item:edit")
    @PostMapping("/changeDeductFlags")
    @ResponseBody
    public AjaxResult changeDeductFlags(GradeItem gradeItem)
    {
        return toAjax(gradeItemService.updateGradeDeductFlag(gradeItem));
    }
}
